package main

import (
	"fmt"
	"log"
	"path"
	//"io"
	"io/ioutil"
	//"log"
	"net/http"
	"os"
	//"path"
	"errors"
	"strconv"

	//"github.com/PuerkitoBio/goquery"
)

var ServerUrl = "http://gbcinternetenforcement.net"

func GenUrls() ([]string, []string) {
	var urls []string
	var dirs []string
	max := 15000
	for i := 11212; i <= max; i++ {
		url := ServerUrl + "/16-" + strconv.Itoa(i)
		dir := "16/" + strconv.Itoa(i)
		dirs = append(dirs, dir)
		urls = append(urls, url)
	}
	return urls, dirs
}

func test(url string, dir string) (string, error) {
	response, err := http.Get(url)
	if err != nil {
		return "", err
	}
	if response != nil && response.Body != nil {
		defer response.Body.Close()
	}

	log.Println(response.StatusCode)
	//	fmt.Println(response.StatusCode)
	if response.StatusCode != http.StatusOK {
		return "", errors.New(response.Status)
	}

	fileFulName := dir + ".html"
	log.Println(fileFulName)

	fileDir := path.Dir(fileFulName)
	log.Println(fileDir)

	fileName := path.Base(fileFulName)
	log.Println(fileName)

	os.MkdirAll(fileDir, 0777)
	file, _ := os.Create(fileFulName)
	//io.Copy(file, response.Body)

	body, _ := ioutil.ReadAll(response.Body)

	file.WriteString(string(body))

	return string(body), nil
}

func main() {
	log.SetFlags(log.Ldate | log.Lshortfile | log.LstdFlags)

	urls, dirs := GenUrls()
	for index, url := range urls {
		test(url, dirs[index])
		fmt.Println(index)
	}
}
